package com.koalac.ironman.common.adapter;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

public class CommonExpandableAdapter<G,C> extends BaseExpandableListAdapter implements NotifyDataSetChanged{
	
	protected int groupXmlName;
	protected int childXmlName;
	protected List<G> groupList;
	protected List<List<C>> childList;
	protected Context context;
	private Map<String,Object> groupParams;
	private Map<String,Object> childParams;
	protected NotifyDataLoadFinished notifyDataLoadFinished;
	protected Class<? extends ViewHolderModel<G>> groupClazz;
	protected Class<? extends ViewHolderModel<C>> childClazz;
	
	
	public CommonExpandableAdapter(Class<? extends ViewHolderModel<G>> groupClazz,Class<? extends ViewHolderModel<C>> childClazz,Context context,List<G> groupList,List<List<C>> childList,
			int groupXmlName,int childXmlName){
		this.context = context;
		this.groupList = groupList;
		this.childList = childList;
		this.groupXmlName = groupXmlName;
		this.childXmlName = childXmlName;
		this.groupClazz = groupClazz;
		this.childClazz = childClazz;
	}


	@Override
	public int getGroupCount() {
		return groupList.size();
	}


	@Override
	public int getChildrenCount(int groupPosition) {
		return childList.get(groupPosition).size();
	}


	@Override
	public List<C> getGroup(int groupPosition) {
		return childList.get(groupPosition);
	}


	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList.get(groupPosition).get(childPosition);
	}


	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}


	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}


	@Override
	public boolean hasStableIds() {
		return false;
	}


	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if(getGroup(groupPosition) != null){
			ViewHolderModel<G> instance = null;
		if(convertView == null){
			convertView = preInitViewHolder(convertView,groupXmlName);
			try{
				Class<?>[] parameterTypes = { Context.class, List.class };
				Constructor<? extends ViewHolderModel<G>> constructor = groupClazz.getConstructor(parameterTypes);
				Object[] parameters = { context, groupList };
				instance = constructor.newInstance(parameters);
				instance.setNotifyDataSetChanged(this);
//				instance.setCallback(callback);待修改
				instance.initViewHoler(groupPosition, convertView);
				convertView.setTag(instance);
			} catch (InstantiationException e) {
				Log.e("创建实例-1-InstantiationException", e.getMessage(), e);
			} catch (IllegalAccessException e) {
				Log.e("创建实例-2-IllegalAccessException", e.getMessage(), e);
			} catch (SecurityException e) {
				Log.e("创建实例-3-SecurityException", e.getMessage(), e);
			} catch (NoSuchMethodException e) {
				Log.e("创建实例-4-NoSuchMethodException", e.getMessage(), e);
			} catch (IllegalArgumentException e) {
				Log.e("创建实例-5-IllegalArgumentException", e.getMessage(), e);
			} catch (InvocationTargetException e) {
				Log.e("创建实例-6-InvocationTargetException", e.getMessage(), e);
			}
		}else{
			instance = (ViewHolderModel<G>) convertView.getTag();
		}
		if (convertView != null && instance != null) {
			instance.setParams(groupParams);
			instance.setViewHolerValues(groupPosition, convertView);
			if (groupPosition == groupList.size() - 1 && notifyDataLoadFinished != null) {
				notifyDataLoadFinished.onDataLoadFinished(groupPosition);
			}
			if(isExpanded){
				
			}
		}
	}
		return convertView;
	}


	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if(getChild(groupPosition, childPosition) != null){
			ViewHolderModel<C> instance = null;
			if(convertView == null){
				convertView = preInitViewHolder(convertView,childXmlName);
				try{
					Class<?>[] parameterTypes = { Context.class, List.class };
					Constructor<? extends ViewHolderModel<C>> constructor = childClazz.getConstructor(parameterTypes);
					Object[] parameters = { context, childList.get(groupPosition) };
					instance = constructor.newInstance(parameters);
					instance.setNotifyDataSetChanged(this);
//					instance.setCallback(callback);待修改
					instance.initViewHoler(childPosition, convertView);
					convertView.setTag(instance);
				} catch (InstantiationException e) {
					Log.e("创建实例-1-InstantiationException", e.getMessage(), e);
				} catch (IllegalAccessException e) {
					Log.e("创建实例-2-IllegalAccessException", e.getMessage(), e);
				} catch (SecurityException e) {
					Log.e("创建实例-3-SecurityException", e.getMessage(), e);
				} catch (NoSuchMethodException e) {
					Log.e("创建实例-4-NoSuchMethodException", e.getMessage(), e);
				} catch (IllegalArgumentException e) {
					Log.e("创建实例-5-IllegalArgumentException", e.getMessage(), e);
				} catch (InvocationTargetException e) {
					Log.e("创建实例-6-InvocationTargetException", e.getMessage(), e);
				}
			}else {
				instance = (ViewHolderModel<C>) convertView.getTag();
			}
			if (convertView != null && instance != null) {
				instance.setParams(childParams);
				instance.list = childList.get(groupPosition);//对 对象进行复用时，需要更新对象所储存的旧数据，把这句去掉会导致数据越界，原因是旧数据被新数据引用出错
				if(childPosition < childList.get(groupPosition).size()){
					instance.setViewHolerValues(childPosition, convertView);
				}
				if (childPosition == childList.get(groupPosition).size() - 1 && notifyDataLoadFinished != null) {
					notifyDataLoadFinished.onDataLoadFinished(childPosition);
				}
			}
		}
		return convertView;
	}

	protected View preInitViewHolder(View convertView,int customXmlName) {
		convertView = LayoutInflater.from(context).inflate(customXmlName, null);
		return convertView;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void addGroupList(List<G> list){
		groupList.addAll(list);
	}
	
	public void addChildList(List<List<C>> list){
		childList.addAll(list);
	}
	
	public void clear(){
		groupList.clear();
		childList.clear();
	}
	
	@Override
	public void setNotifyDataSetChanged() {
		
	}


	@Override
	public void seNotifyDataSetInvalidated() {
		
	}

}
